﻿$(function () {
    var dialog = $("<div />").dialog({
        title: "登录",
        content: $("#login-model").show(),
        width: 255, cache: false, closable: false,
        buttons: [
            { text: "登录", handler: function () { $.Login(); } }
        ],
        onLoad: function () { dialog.dialog("center"); }
    });
    $(window).resize(function () { dialog.dialog("center"); });

    var img = $("<img id='validate-img' class='validate-img' src='ValidateCode.aspx' />");
    img.click(function () { $.ClearValidate(); });
    var box = $("<span class='validate-input combo'></span>")
    box.css({ "width": (56 - 2) + "px", "height": "22px" });
    var input = $("<input id='validate-code' maxlength='4' />");
    $(input).addClass("validatebox-text").addClass("combo-text");
    $(input).css({ "height": "22px", "line-height": "22px", "text-align": "center" });
    $(input).css({ "border": "none", "width": (56 - 6) + "px" }).appendTo(box);

    input.keyup(function () { if (this.value.length === 4) { $.Login(); } });

    $(".dialog-button").prepend(box).prepend(img);
    $(input).validatebox({ required: true, validType: "minLength[4]" });
});

$.extend({
    //写入等待
    ShowMask: function () {
        var body = $("body").eq(0);
        var mask = $("<div class=\"datagrid-mask\" style=\"display:block; z-index:77777;\"></div>").appendTo(body);
        var img = $("<img class=\"datagrid-mask-img\" src='admin/images/loading.gif' style=\"z-index:88888;\" />");
        img.css("left", (body.width() - 214) / 2).css("top", 12).css("position", "absolute").appendTo(body);
    },
    //Api路径
    Api: function (path) {
        return "public/ApiHandler.ashx?path=" + path;
    },
    //表单实体
    ModelForm: function (options) {
        var submit = function () {
            var bool = $(this).form("validate");
            bool = $("#validate-code").validatebox("isValid");
            if (!bool) { $("#validate-code").focus(); }
            $("#hd_validate").val($("#validate-code").val());
            if (bool) $.ShowMask(); return bool;
        }
        $(options.id).form("submit", {
            url: options.url, onSubmit: submit,
            success: function (result) {
                $.HideMask();
                try {
                    result = JSON.parse(result);
                    options.success(result);
                } catch (e) { $.Error(result); }
            }
        });
    },
    //登陆
    Login: function () {
        $.ModelForm({
            url: $.Api("Admin/Manage/Login"),
            id: "#login-model",
            success: function (result) {
                if (result.Success) {
                    location.href = "admin/index.aspx"; return false;
                }
                else {
                    $.ClearValidate();
                    $.EasyuiSlide(result.Message); 
                }
            }
        });
    },
    //清空验证码
    ClearValidate: function () {
        $("#validate-code").val("");
        $("#validate-img").attr('src', 'ValidateCode.aspx?rd=' + new Date().getTime());
    }
});
